//
//  DeviceListViewController.swift
//  MZBluePrinter
//
//  Created by Angus on 15/11/30.
//  Copyright © 2015年 MZ. All rights reserved.
//

import UIKit

class DeviceCell: UITableViewCell {
    
    @IBOutlet weak var deviceName: UILabel!
    @IBOutlet weak var deviceID: UILabel!
    
    
    func configure(device:Device?) {
        
        deviceName.text = device?.deviceName
        deviceID.text   = device?.uuidString
    }
}

class DeviceListViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{
    
    
    @IBOutlet weak var tableView: UITableView!
    
    var devices:[Device] = []
    
    
    //MARK: Life Cyc
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tableView.dataSource = self
        self.tableView.delegate   = self
        
        BLEManager.shareInstance().completion_error = { [unowned self] (error) in
            self.removeAllOverlays()
            BLEManager.shareInstance().disconnect()
        }
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        BLEManager.shareInstance().startScanDevice { [unowned self](array) -> Void in
            if (array.count > 0) {
                self.devices.removeAll()
                self.devices += array as! [Device]
                self.tableView.reloadData()
            }
        }
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        BLEManager.shareInstance().stopScanDevice()
    }
    
    deinit{
        self.tableView.dataSource = nil
        self.tableView.delegate   = nil
    }
    
    //MARK: Response Action
    @IBAction func backButtonClick(sender: AnyObject) {
        self.navigationController?.popViewControllerAnimated(true)
    }
    
    //MARK: UITableViewDataSourve
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return devices.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:DeviceCell = tableView.dequeueReusableCellWithIdentifier("Device", forIndexPath: indexPath) as! DeviceCell
        cell.configure(self.devices[indexPath.row])
        return cell
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 75.0
    }
    
    //MARK: UITableViewDelegate
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let device:Device = self.devices[indexPath.row]
        self.mz_showWaitOverlay()
        BLEManager.shareInstance().connectDevice(device) {[unowned self] (error) -> Void in
            
            self.mz_removeAllOverlays()
            if error == nil {
 
                self.navigationController?.popViewControllerAnimated(true)
            } else {
                self.mz_showAlert(title: "Error", message: "Conntect Fail!")
            }
        }
    }
    
 
    
}
